The
SSIS Wizard is a streamlined interface solely used to generate SSIS
packages for importing or exporting data. It is really quite powerful
and provides an easy but sophisticated way to move data from or to any
OLE DB, ODBC, or text source to another OLE DB, ODBC, or text source.
You can also define simple or complex data transformations using the
many options provided by the wizard. The wizard can also copy database
schema, but the transfer of all other database objects, such as
indexes, constraints, users, permissions, stored procedures, and so on,
is supported only between SQL Server 7.0 and higher SQL Servers.
The SSIS Import/Export Wizard takes the user through five basic steps for both imports and exports:
1. | Select/identify the data source (source).
| 2. | Select/identify the destination (target).
| 3. | Select
the data copy and transformation type. The options are to copy data
with or without the schema, to move data based on a query, or to
transfer objects and data between data stores.
| 4. | Define any data transformations, if required.
| 5. | Save, schedule, and execute the package.
|
Let’s walk through a
quick wizard sequence and create a package that fulfills the “Hot
Customers Plus” data movement/transformation requirement. You will be
pulling and transforming data from the AdventureWorks2008 database and pushing it to another database on the same server instance (SQL08DEV01 in this example). So, first, you need to create a database named UnleashedDataMart as the target database to hold the new HotCustomersPlus
table you will be creating with SSIS. Remember that you use the SSIS
Wizard for simple package creations (or data transfers). Nothing fancy
here. To get started, here’s what you do:
1. | Fire
up the SSIS Wizard from within SSMS by either right-clicking the
database branch for the database from which you will be exporting data
or right-clicking in the summary pane for that same database (as shown
in Figure 1).
If you select the Tasks option, you are given the option of either
importing or exporting data. You want to export data from this
database, so choose the Export Data option.
| 2. | Work
through the steps of the SSIS Wizard. The initial step is identifying
the source for the data. In this example, you need to choose a valid
SQL Server and source database (in this example, DBARCH-LT2\SQL08DE01 for the server and AdventureWorks2008
for the database). In addition, you must provide the appropriate access
credentials (Windows authentication or SQL Server authentication) for
this source SQL Server. You have a few options of exactly what access
mechanism to use (to this data source). Choose the SQL Native Client
connection method (see Figure 2).
| 3. | Next is the data “destination” specification (the target). We had already created a new database (called UnleashedDataMart) for this purpose before we started and use that for this example. Our example uses the same SQL Server instance of DBARCH-LT2\SQL08DE01, using the connection method to this SQL Server instance of SQL Native Client, and the previously mentioned database of UnleashedDataMart (as shown in Figure 3). We also use Windows Authentication. You are finished with this window, so click Next.
| 4. | The
next step in the wizard asks if you will be pulling source data from
one or more tables (or views) or if you will be specifying a SQL query
to pull data from the source. For this example, select the Write a
Query to Specify the Data to Transfer option because this approach best
fits the requirement specified earlier (see Figure 4).
| 5. | In
the next step in the wizard, create your custom SQL statement that will
be used to select data from the source database. We have provided a
fairly complex SQL query that selects (and joins) data from six tables
in the AdventureWorks2008 database to fulfill the data
requirement for this example. This SQL Statement is available on this
book’s CD. In this step of the wizard, enter the following query:
SELECT a.CustomerID, e.name as CustomerName, a.CustomerType, a.TerritoryID, d.name as TerritoryName, c.ProductID, f.name as ProductName, sum(c.LineTotal) as YTDSalesTotal FROM [sales].[Customer] a INNER JOIN [sales].[SalesOrderHeader] b ON a.customerid = b.customerid INNER JOIN [sales].[SalesOrderDetail] c ON b.SalesOrderID = c.SalesOrderID INNER JOIN [sales].[SalesTerritory] d ON a.TerritoryID = d.TerritoryID INNER JOIN [sales].[Store] e ON a.customerid = e.customerid INNER JOIN [Production].[Product] f ON c.productID = f.ProductID WHERE b.orderdate >= '2004-01-01 00:00:00.000' GROUP BY a.customerID, e.name, a.CustomerType, a.TerritoryID, d.name, c.ProductID, f.name HAVING sum(c.LineTotal) > 5000 ORDER BY d.name, e.name
In this window, you can select a query from a file by clicking the
Browse button to search for this file, or you can simply start coding
directly in the window. You can click the Parse button to guarantee
that the SQL statement has valid syntax and form (see Figure 5). You can test it (preview the data) in the next wizard step.
However, if you chose to copy data directly from the database tables
(and not use a SQL query), you would be provided a list of tables and
views from the source database and would be able to map one or more of
these tables to tables on the destination database. Figure 6 shows how this Select Source Tables and Views window would look.
| 6. | Rename the destination table by changing [dbo].[Query] to [UnleashedDataMart].[dbo].[HotCustomersPlus] (see Figure 7). All subsequent references to this destination target will be what you want.
| 7. | Click the Preview button on this dialog to actually execute the SQL query specified in step 5. Figure 8
shows the Preview Data results of the custom SQL query. Close this data
preview window when you are finished reviewing the data results.
| 8. | Click
the Edit Mappings button to see the details of the column-level
mappings being defined. At this point, you can further subset the
columns, change data types, use precision or scale change, and/or not
have a column mapped during the data transformation. As you can see in Figure 9,
the Source and Destination columns are side by side, and when you click
a column name, you can adjust what you want to occur (such as ignore or
map to the column). In addition, at the object level, you can have the
table created at the destination, have it truncate the data in an
existing table at the destination, or append data to existing data at
the destination. For this example, choose to completely drop and
re-create the destination table each time. You are basically done
creating the logic and data mappings for this simple data
transformation.
| 9. | If you want, click the Edit SQL button in the Column Mappings dialog. A CREATE TABLE SQL statement appears, and you can modify it if you want (see Figure 10). You don’t need to do any further changes at this time, however.
| 10. | In
the Save and Run Package dialog that appears, choose Run Immediately
and Save SSIS Package. Save the SSIS package in SQL Server in the msdb SQL Server database (see Figure 11). It is also possible to save the SSIS package in a structured storage file at the filesystem level (in a .dtsx file).
| 11. | In
the Save SSIS Package dialog, you specify the name of the package,
description of the package, and location where the package is to be
stored. For this example, specify the name “HotCustomersPlus” for the
SSIS package, as shown in Figure 12.
| 12. | When
the SSIS Wizard displays the Complete the Wizard dialog, summarizing
all the actions to be taken, carefully review the list and then click
Finish when you are ready to proceed. After you click Finish, the
wizard’s execution console appears, as shown in Figure 13. This console shows all the steps taken, the status of these steps, and informational detail, as required. In Figure 13 note the Copying to [Unleashed].[dbo].[HotCustomersPlus]
table message that 943 rows were transferred. There is an Error in the
step above the row copy that refers to the Drop of the target table not
being found. This was expected since the table never existed before,
this was the first time it would have been created. Subsequent runs
will not have this error. Following this particular “Copying” step are
the simple post execute and cleanup actions for the package.
|
If you would like, you
can also query the system catalog table that contains the metadata for
packages. In this case, the system table [msdb].[dbo].[sysssispackages]
contains this metadata for SSIS packages starting with SQL Server 2005,
and going forward. The following simple SQL query shows the metadata
entry for the package you just created:
SELECT * FROM [msdb].[dbo].[sysssispackages]
The results look like this:
Name ID Description Datetime HotCustomersPlus 025EBC25... Weekly datamart updates... 2009-11-01 ...
Figure 14
shows the execution tasks for doing straight table copying
(transferring) using SSIS packages of the tables to create a
quick-and-dirty (refreshable) data mart. This method of spinning off
data quickly is very useful, and it fits our requirements.
|